<?php
namespace App\Http\Middleware\Search\Keyword;

use Closure;
use App\Services\Trade\SearchService;

/**
 * 搜索关键词记录
 */
class Trade
{

    /**
     * 前置 | 后置
     *
     * @param \Illuminate\Http\Request $request            
     * @param \Closure $next            
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    /**
     * 响应发送到浏览器之后处理
     *
     * @param \Illuminate\Http\Request $request            
     * @param \Illuminate\Http\Response $response            
     */
    public function terminate($request, $response)
    {
        // 参数是否存在
        $keyword = $request->query('keyword');
        if (! $keyword) {
            return false;
        }
        // 记录关键词
        $searchService = new SearchService();
        $searchService->saveRecord($keyword);
    }
}